import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lottie/lottie.dart';

class LottieUtils extends StatelessWidget {
  final Widget child;
  final bool isLoading;

  // 加载动画是否覆盖在原有界面上
  final bool? cover = true;

  const LottieUtils({Key? key, required this.child, required this.isLoading}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (cover!) {
      return Stack(
        children: [child, isLoading ? _loadingView : Container()],
      );
    } else {
      return isLoading ? _loadingView : child;
    }
  }

  static Widget get _loadingView {
    // return Center(
    //   child: Lottie.network('https://assets3.lottiefiles.com/private_files/lf30_gvdwzaoj.json'),
    // );
    // return Center(child: Lottie.asset("assets/loading.json"));
    return Center(
      child: Container(
        color: Colors.transparent,
        height: ScreenUtil().screenHeight,
        width: ScreenUtil().screenWidth,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Container(
              decoration: BoxDecoration(
                color: const Color(0x4d999999),
                // color: Colors.transparent,
                borderRadius: BorderRadius.all(Radius.circular(50.r)),
              ),
              width: 100.r,
              height: 100.r,
              child: Center(
                child: SizedBox(
                  width: 100.r,
                  height: 100.r,
                  child: Lottie.asset("assets/loading.json", frameRate: FrameRate.max),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  static showToastLoading() {
    BotToast.showCustomLoading(
        toastBuilder: (cancelFunc) {
          return _loadingView;
        },
        duration: const Duration(seconds: 10));
  }

  static hideToastLoading() {
    BotToast.closeAllLoading();
  }
}
